# Various mash calculations
module Mash

  # Calculates strike water temperature
  #
  # @note
  #     Temperatures must be measured in Celsius, ratio is liters per kilograms
  #
  # @example
  #     Mash.strike_water({
  #       initial: 20, # temperature of grain; i.e. ambient temperature
  #       target: 60,  # target mash temperature
  #       ratio: 1.25  # water to grain ratio; e.g. 1.25L/kg
  #     })
  #
  #     Mash.strike_water({
  #       initial: 20,
  #       target: 60,
  #       ratio: 1.25,
  #       adjustment: 1.015 # calculated temperature is multiplied by the adjustment to compensate for heat lost to the mash tun
  #     })
  #
  # @param [Hash] data data required to calculate strike temperature; `initial`, `target`, `ratio` and optionally `adjustment`
  # @return [Fixnum] strike water temperature
  def self.strike_temperature data
    adjustment = data[:adjustment] || 1
    target = data[:target]

    (((0.41 / data[:ratio]) *
      (target - data[:initial]) +
       target) * adjustment).round
  end

  # Calculates volume of boiling water needed to increase mash temperature
  #
  # @note
  #     Temperatures must be measured in Celsius, mass in kilograms and volume in liters
  #
  # @example
  #     Mash.infusion_volume({
  #       initial: 40, # current mash temperature
  #       target: 60,  # target mash temperature
  #       mass: 4,     # grain mass in mash
  #       volume: 6    # water volume in mash
  #     })
  #
  # @param [Hash] data data required to calculate strike temperature; `initial`, `target`, `mass`, `volume`
  # @return [Float] liters of infusion water needed for step
  def self.infusion_volume data
    target = data[:target]

    ((target - data[:initial]) *
    ((0.41 * data[:mass]) + data[:volume]) /
     (100 - target)).round 1
  end

end
